package com.atguigu.day26.problem03;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class ProblemTest01 {
    public static void main(String[] args) throws Exception{

        //1.创建Scanner对象
        Scanner input = new Scanner(System.in);
        //2.获取输入id值
        System.out.println("请您输入id");
        String messageId = input.nextLine();
        //3.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //4.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "root");
        //5.准备sql
        String sql = "select * from t_account where aid = ?";
        //select * from t_account where aid = 99 or 1=1
        System.out.println("sql = " + sql);
        //6.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //todo 给第一个?赋值  值是messageId
        pst.setObject(1,messageId);
        //7.执行命令获取结果集
        ResultSet rs = pst.executeQuery();
        //8.处理结果集展示数据
        while (rs.next()){
            Object aid = rs.getObject(1);
            Object name = rs.getObject(2);
            Object balance = rs.getObject(3);
            System.out.println(aid+" "+name+" "+balance);

        }

        //9.关闭资源
        rs.close();
        pst.close();
        connection.close();
        input.close();

    }


   /* public static void main(String[] args)  throws Exception{
        //1.创建Scanner对象
        Scanner input = new Scanner(System.in);
        //2.获取输入id值
        System.out.println("请您输入id");
        String messageId = input.nextLine();
        //3.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //4.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "root");
        //5.准备sql
        String sql = "select * from t_account where aid = "+messageId;
        //select * from t_account where aid = 99 or 1=1
        System.out.println("sql = " + sql);
        //6.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //7.执行命令获取结果集
        ResultSet rs = pst.executeQuery();
        //8.处理结果集展示数据
        while (rs.next()){
            Object aid = rs.getObject(1);
            Object name = rs.getObject(2);
            Object balance = rs.getObject(3);
            System.out.println(aid+" "+name+" "+balance);

        }

        //9.关闭资源
        rs.close();
        pst.close();
        connection.close();
        input.close();
    }*/
}
